package com.kcs.dc.service.logic;


import java.sql.Connection;
import java.util.ArrayList;
import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.LogWriter;
import com.kcs.common.utils.commonUtils;

public abstract class generateResponse {
		
	protected boolean isHeaderValid(String system, String serviceID, String requestID) {
		
		boolean somethingNull = (isNullOrBlankString(system) || 
								 isNullOrBlankString(serviceID) || 
								 isNullOrBlankString(requestID));

		if (somethingNull) return false;
							
		return true;
		
	}
    
    protected boolean isNullOrBlankString(String str) {
        return str == null || str.equals("");
    }
	
    /*Check Authentication of Front System.*/
    protected boolean isAuthenSystem(Connection conn, String system, String serviceID){
    	
    	StringBuffer sqlExist = new StringBuffer();
    	sqlExist.append("SELECT COUNT(*) CNT ");
    	sqlExist.append("FROM CONFIG_SYS_SERVICE ");
    	sqlExist.append("WHERE ACTIVE_STATUS = 'Y' ");
    	sqlExist.append("AND SYSTEM_ID = ? ");
    	sqlExist.append("AND SERVICE_ID = ? ");
   	
		LogWriter.debug("sqlExist is " + sqlExist.toString(), Constant.LOGGER_WEBSERVICE);
		ArrayList<String> cond = new ArrayList<String>();
		cond.add(system);
		cond.add(serviceID);
		
    	if(commonUtils.isExistData(sqlExist.toString(), cond, conn))
        	return true;
        else
        	return false;
	
    }
    
    /*Check Authentication of RequestID.*/
    protected boolean isAuthenRequest(Connection conn, String requestID, String system, String serviceID){
    	
    	StringBuffer sqlExist = new StringBuffer();
    	sqlExist.append("SELECT COUNT(*) CNT " );
    	sqlExist.append("FROM SERVICE_LOG ");
    	sqlExist.append("WHERE req_id = ? " );
    	sqlExist.append("AND req_system_id = ? ");
    	sqlExist.append("AND req_service_id = ? ");
   	
		LogWriter.debug("sqlExist is " + sqlExist.toString(), Constant.LOGGER_WEBSERVICE);
		ArrayList<String> cond = new ArrayList<String>();
		cond.add(requestID);
		cond.add(system);
		cond.add(serviceID);
		
    	if(commonUtils.isExistData(sqlExist.toString(), cond, conn))
        	return false;
        else
        	return true;
	
    }    
      	   
    protected Connection getConnectionDealCenter(){
    	
    	Connection msCon = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);

		return msCon; 
    }
    
    protected Connection getConnectionRETAD(){
    	
    	Connection oraCon = ConnectionUtils.getConnection(Constant.ORACLE, Constant.FROM);
    	
    	return oraCon;
    }
}
